﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.ObjectModel;
using System.Web;
using System.Configuration;
using VioletDevelopmentLaboratory.Net.Web.Configuration;
using VioletDevelopmentLaboratory.Net.Data;

namespace VioletDevelopmentLaboratory.Net.Web.Component
{
	public class BoardComment
	{
		private String applicationName;
		/// <summary>
		/// 현재 댓글을 포함하는 웹에플리케이션의 ApplicationName을 가져옵니다.
		/// </summary>
		public String ApplicationName { get { return this.applicationName; } internal set { this.applicationName = value; } }

		private String componentID;
		/// <summary>
		/// 현재 댓글을 포함하는 콤포넌트의 아이디를 가져옵니다.
		/// </summary>
		public String ComponentID { get { return this.componentID; } internal set { this.componentID = value; } }

		private String threadID;
		/// <summary>
		/// 현재 댓글을 포함하는 글의 아이디를 가져옵니다.
		/// </summary>
		public String ThreadID { get { return this.threadID; } internal set { this.threadID = value; } }

		private String commentID;
		/// <summary>
		/// 아이디를 가져옵니다.
		/// </summary>
		public String CommentID { get { return this.commentID; } internal set { this.commentID = value; } }

		private String userName;
		/// <summary>
		/// 작성자 이름을 가져옵니다.
		/// </summary>
		public String UserName { get { return this.userName; } internal set { this.userName = value; } }

		private String providerUserKey;
		/// <summary>
		/// 작성자 공급자키를 가져옵니다.
		/// </summary>
		public String ProviderUserKey { get { return this.providerUserKey; } internal set { this.providerUserKey = value; } }

		private Boolean isBlocked;
		/// <summary>
		/// 블라인드 상태인지 가져옵니다.
		/// </summary>
		public Boolean IsBlocked { get { return this.isBlocked; } internal set { this.isBlocked = value; } }

		private String ip;
		/// <summary>
		/// 작성자 아이피주소를 가져오거나 설정합니다.
		/// </summary>
		public String IP { get { return String.IsNullOrEmpty(this.ip) ? "0.0.0.0" : this.ip; } set { this.ip = value; } }

		private DateTime registrationDate;
		/// <summary>
		/// 등록 날짜시간을 가져옵니다.
		/// </summary>
		public DateTime RegistrationDate { get { return this.registrationDate; } internal set { this.registrationDate = value; } }

		private String lastUpdateBy;
		/// <summary>
		/// 마지막으로 수정한 사용자의 이름을 가져오거나 설정합니다.
		/// </summary>
		public String LastUpdateBy { get { return this.lastUpdateBy; } set { this.lastUpdateBy = value; } }

		private DateTime lastUpdateDate;
		/// <summary>
		/// 마지막 수정 날짜시간을 가져옵니다.
		/// </summary>
		public DateTime LastUpdateDate { get { return this.lastUpdateDate; } internal set { this.lastUpdateDate = value; } }

		private Boolean isHtml;
		/// <summary>
		/// 내용이 Html형식인지 가져오거나 설정합니다.
		/// </summary>
		public Boolean IsHtml { get { return isHtml; } set { this.isHtml = value; } }

		private String content;
		/// <summary>
		/// 내용을 가져오거나 설정합니다.
		/// </summary>
		public String Content
		{
			get
			{
				if (this.IsBlocked)
					return VUtility.GetVWebGlobalResource("Board__BlockedCommentTitle");

				return this.content;
			}
			set { this.content = value; }
		}

		private String pageUrl;
		/// <summary>
		/// 링크를 가져옵니다.
		/// </summary>
		public String PageUrl { get { return this.pageUrl; } internal set { this.pageUrl = value; } }

		public BoardCommentUpdateResult Modify()
		{
			Object returnValue = null;

			SqlClientAgent.ExecuteNonQuery(
				ConfigurationManager.ConnectionStrings[VWebConfigurationReader.ConnectionStringName].ConnectionString,
				CommandType.StoredProcedure,
				"VWeb_BoardComment_Update",
				out returnValue,
				new SqlParameter("@ApplicationName", VWebConfigurationReader.ApplicationName),
				new SqlParameter("@ComponentID", this.ComponentID),
				new SqlParameter("@ThreadID", this.ThreadID),
				new SqlParameter("@CommentID", this.CommentID),
				new SqlParameter("@IP", this.IP),
				new SqlParameter("@UpdateBy", this.LastUpdateBy),
				new SqlParameter("@IsHTML", this.IsHtml),
				new SqlParameter("@Content", this.Content)
			);

			VUtility.SendSmtpMessage("BoardComment.Modify", this.IP, this.LastUpdateBy, "", this.Content);

			switch ((Int32)returnValue)
			{
				case 0:
					return BoardCommentUpdateResult.Success;
				default:
					return BoardCommentUpdateResult.InvalidComment;
			}
		}

		public BoardCommentDeleteResult Remove()
		{
			Object returnValue = null;

			SqlClientAgent.ExecuteNonQuery(
				ConfigurationManager.ConnectionStrings[VWebConfigurationReader.ConnectionStringName].ConnectionString,
				CommandType.StoredProcedure,
				"VWeb_BoardComment_Delete",
				out returnValue,
				new SqlParameter("@ApplicationName", VWebConfigurationReader.ApplicationName),
				new SqlParameter("@ComponentID", this.ComponentID),
				new SqlParameter("@ThreadID", this.ThreadID),
				new SqlParameter("@CommentID", this.CommentID)
			);

			switch ((Int32)returnValue)
			{
				case 0:
					return BoardCommentDeleteResult.Success;
				default:
					return BoardCommentDeleteResult.InvalidComment;
			}
		}

		public BoardCommentSetClearResult SetClear()
		{
			Object returnValue = null;

			SqlClientAgent.ExecuteNonQuery(
				ConfigurationManager.ConnectionStrings[VWebConfigurationReader.ConnectionStringName].ConnectionString,
				CommandType.StoredProcedure,
				"VWeb_BoardComment_SetClear",
				out returnValue,
				new SqlParameter("@ApplicationName", VWebConfigurationReader.ApplicationName),
				new SqlParameter("@ComponentID", this.ComponentID),
				new SqlParameter("@ThreadID", this.ThreadID),
				new SqlParameter("@CommentID", this.CommentID)
			);

			switch ((Int32)returnValue)
			{
				case 0:
					return BoardCommentSetClearResult.Success;
				default:
					return BoardCommentSetClearResult.InvalidComment;
			}
		}

		public BoardCommentSetBlockResult SetBlock()
		{
			Object returnValue = null;

			SqlClientAgent.ExecuteNonQuery(
				ConfigurationManager.ConnectionStrings[VWebConfigurationReader.ConnectionStringName].ConnectionString,
				CommandType.StoredProcedure,
				"VWeb_BoardComment_SetBlock",
				out returnValue,
				new SqlParameter("@ApplicationName", VWebConfigurationReader.ApplicationName),
				new SqlParameter("@ComponentID", this.ComponentID),
				new SqlParameter("@ThreadID", this.ThreadID),
				new SqlParameter("@CommentID", this.CommentID)
			);

			switch ((Int32)returnValue)
			{
				case 0:
					return BoardCommentSetBlockResult.Success;
				default:
					return BoardCommentSetBlockResult.InvalidComment;
			}
		}






		internal static BoardComment Fill(SqlDataReader reader)
		{
			BoardComment comment = new BoardComment();
			comment.ApplicationName = reader["ApplicationName"].ToString();
			comment.ComponentID = reader["ComponentID"].ToString();
			comment.ThreadID = reader["ThreadID"].ToString();
			comment.CommentID = reader["CommentID"].ToString();
			comment.UserName = reader["UserName"].ToString();
			comment.ProviderUserKey = reader["ProviderUserKey"].ToString();
			comment.IsBlocked = (Boolean)reader["IsBlocked"];
			comment.IP = reader["IP"].ToString();
			comment.RegistrationDate = (DateTime)reader["RegistrationDate"];
			comment.LastUpdateBy = reader["LastUpdateBy"].ToString();
			comment.LastUpdateDate = (reader["LastUpdateDate"] != DBNull.Value) ? (DateTime)reader["LastUpdateDate"] : new DateTime(0L);
			comment.IsHtml = (Boolean)reader["IsHTML"];
			comment.Content = reader["Content"].ToString();
			comment.PageUrl = BoardUtil.GetBoardUrl(reader["PageUrl"].ToString(), BoardPart.View, BoardParameters.PageIndex, BoardParameters.CategoryID, comment.ThreadID, BoardParameters.SearchText, 0, 0) + "#" + comment.CommentID;
			return comment;
		}

		internal static BoardComment Fill(DataRow row)
		{
			BoardComment comment = new BoardComment();
			comment.ApplicationName = row["ApplicationName"].ToString();
			comment.ComponentID = row["ComponentID"].ToString();
			comment.ThreadID = row["ThreadID"].ToString();
			comment.CommentID = row["CommentID"].ToString();
			comment.UserName = row["UserName"].ToString();
			comment.ProviderUserKey = row["ProviderUserKey"].ToString();
			comment.IsBlocked = (Boolean)row["IsBlocked"];
			comment.IP = row["IP"].ToString();
			comment.RegistrationDate = (DateTime)row["RegistrationDate"];
			comment.LastUpdateBy = row["LastUpdateBy"].ToString();
			comment.LastUpdateDate = (row["LastUpdateDate"] != DBNull.Value) ? (DateTime)row["LastUpdateDate"] : new DateTime(0L);
			comment.IsHtml = (Boolean)row["IsHTML"];
			comment.Content = row["Content"].ToString();
			comment.PageUrl = BoardUtil.GetBoardUrl(row["PageUrl"].ToString(), BoardPart.View, BoardParameters.PageIndex, BoardParameters.CategoryID, comment.ThreadID, BoardParameters.SearchText, 0, 0) + "#" + comment.CommentID;
			return comment;
		}

		public static BoardCommentCollection GetLastCommentCollection(Int32 maximumRows)
		{
			if (maximumRows < 1)
				throw new ArgumentOutOfRangeException("maximumRows");

			SqlDataReader reader = SqlClientAgent.ExecuteReader(
				ConfigurationManager.ConnectionStrings[VWebConfigurationReader.ConnectionStringName].ConnectionString,
				CommandType.StoredProcedure,
				"VWeb_BoardComment_GetBoardCommentSummary",
				new SqlParameter("@ApplicationName", VWebConfigurationReader.ApplicationName),
				new SqlParameter("@MaximumRows", maximumRows)
			);

			BoardCommentCollection commentCollection = null;

			try
			{
				if (reader.HasRows)
				{
					commentCollection = new BoardCommentCollection();
					BoardComment comment = null;
					while (reader.Read())
					{
						comment = new BoardComment();
						comment.ApplicationName = reader["ApplicationName"].ToString();
						comment.ComponentID = reader["ComponentID"].ToString();
						comment.ThreadID = reader["ThreadID"].ToString();
						comment.CommentID = reader["CommentID"].ToString();
						comment.UserName = reader["UserName"].ToString();
						comment.ProviderUserKey = reader["ProviderUserKey"].ToString();
						comment.IsBlocked = (Boolean)reader["IsBlocked"];
						comment.IP = reader["IP"].ToString();
						comment.RegistrationDate = (DateTime)reader["RegistrationDate"];
						comment.LastUpdateBy = reader["LastUpdateBy"].ToString();
						comment.LastUpdateDate = (reader["LastUpdateDate"] != DBNull.Value) ? (DateTime)reader["LastUpdateDate"] : new DateTime(0L);
						comment.IsHtml = (Boolean)reader["IsHTML"];
						comment.Content = reader["Content"].ToString();
						comment.PageUrl = BoardUtil.GetBoardUrl(reader["PageUrl"].ToString(), BoardPart.View, BoardParameters.PageIndex, BoardParameters.CategoryID, comment.ThreadID, BoardParameters.SearchText, 0, 0) + "#" + comment.CommentID;

						commentCollection.Add(comment);
					}
				}
			}
			finally
			{
				reader.Close();
			}

			return commentCollection;
		}
	}

	public class BoardCommentCollection : Collection<BoardComment> { }
}
